package com.real.operationalcontrols.企业列表;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.bin.david.form.core.SmartTable;
import com.bin.david.form.data.CellInfo;
import com.bin.david.form.data.format.bg.ICellBackgroundFormat;
import com.bin.david.form.data.style.FontStyle;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.real.operationalcontrols.R;
import com.real.operationalcontrols.基类.BaseActivity;
import com.real.operationalcontrols.实体类.WatherHistroyBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.autosize.internal.CancelAdapt;

public class HourHistoryActivity extends BaseActivity implements OnDateSetListener,
        CancelAdapt {

    @BindView(R.id.bar_back_btn)
    ImageView barBackBtn;
    @BindView(R.id.bar_title)
    TextView barTitle;
    @BindView(R.id.bar_check_btn)
    TextView barCheckBtn;
    @BindView(R.id.qian_btn)
    TextView qianBtn;
    @BindView(R.id.time_btn)
    TextView timeBtn;
    @BindView(R.id.hou_btn)
    TextView houBtn;
    @BindView(R.id.table)
    SmartTable table;
    String start_time;
    String finsh_time;
    TimePickerDialog timePickerDialog;
    String id = "";
    List<WatherHistroyBean> data = new ArrayList<>();
    SimpleDateFormat sdf_hour = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat sdf_start = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
    SimpleDateFormat sdf_finish = new SimpleDateFormat("yyyy-MM-dd 23:00:00");
    long baseTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_hour_history);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_hour_history;
    }

    @Override
    protected void getData() {
        barTitle.setText("(水质)小时历史数据");
        id = getIntent().getStringExtra("id");
        timeBtn.setText(sdf_hour.format(new Date()));
        baseTime = System.currentTimeMillis();
        initData();
    }
    private void initData() {

        start_time = sdf_start.format(baseTime);
        finsh_time = sdf_finish.format(baseTime);
        Map<String, String> map = new HashMap<>();
        map.put("MonitorID", id);
        map.put("start", start_time);
        map.put("end", finsh_time);
        map.put("type", "xiaoshi");

        data.clear();
        for (int i = 0; i < 30; i++) {
            WatherHistroyBean beanTest = new WatherHistroyBean();
            beanTest.setTime("2020年1月15日15点42分");
            beanTest.setVoc_one(String.valueOf(new Random().nextInt(100)));
            beanTest.setVoc_two(String.valueOf(new Random().nextInt(400)));
            data.add(beanTest);
        }
        table.setData(data);

        ICellBackgroundFormat<CellInfo> backgroundFormat = new ICellBackgroundFormat<CellInfo>() {
            @Override
            public void drawBackground(Canvas canvas, Rect rect, CellInfo cellInfo, Paint paint) {
                if (cellInfo.row % 2 == 0) {
                    paint.setColor(ContextCompat.getColor(getBaseContext(),
                            R.color.colorTitleLight));
                    canvas.drawRect(rect, paint);
                }
            }

            @Override
            public int getTextColor(CellInfo cellInfo) {
                return Color.BLACK;
            }
        };

        table.getConfig().setContentStyle(new FontStyle(28, Color.BLACK))
                .setColumnTitleStyle(new FontStyle(35, Color.BLUE))
                .setShowXSequence(false)
                .setShowYSequence(false)
                .setShowTableTitle(false)
                .setContentCellBackgroundFormat(backgroundFormat)
                //设置最小宽度防止表格只有几列不美观
                .setMinTableWidth(ScreenUtils.getScreenWidth())
                .setVerticalPadding(30);
    }
    @OnClick({R.id.bar_back_btn, R.id.qian_btn, R.id.time_btn, R.id.hou_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bar_back_btn:
                finish();
                break;
            case R.id.qian_btn:
                baseTime -= 60 * 60 * 1000 * 24;
                timeBtn.setText(sdf_hour.format(baseTime));
                if (baseTime > System.currentTimeMillis()) {
                    return;
                }
                initData();
                break;
            case R.id.time_btn:
                initTimePickView(Type.YEAR_MONTH_DAY);
                break;
            case R.id.hou_btn:
                baseTime += 60 * 60 * 1000 * 24;
                timeBtn.setText(sdf_hour.format(baseTime));
                if (baseTime > System.currentTimeMillis()) {
                    ToastUtils.showShort("超出当前记录时间");
                    return;
                }
                initData();
                break;
        }
    }

    private void initTimePickView(Type type) {
        long tenYears = 10L * 365 * 1000 * 60 * 60 * 24L;
        timePickerDialog = new TimePickerDialog.Builder()
                .setCallBack(this)
                .setCancelStringId("取消")
                .setSureStringId("确认")
                .setTitleStringId("选择时间")
                .setYearText("年")
                .setMonthText("月")
                .setDayText("日")
                .setHourText("时")
                .setMinuteText("分")
                .setCyclic(false)
                .setMinMillseconds(System.currentTimeMillis() - tenYears)
                .setMaxMillseconds(System.currentTimeMillis())
                .setCurrentMillseconds(baseTime)
                .setThemeColor(getResources().getColor(R.color.colorTitle))
                .setType(type)
                .setWheelItemTextNormalColor(getResources().getColor(R.color.timetimepicker_default_text_color))
                .setWheelItemTextSelectorColor(getResources().getColor(R.color.colorTitle))
                .setWheelItemTextSize(12)
                .build();
        timePickerDialog.show(getSupportFragmentManager(), type.toString());
    }

    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        baseTime = millseconds;
        timeBtn.setText(sdf_hour.format(millseconds));
        initData();
    }
}
